package ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import Music_Playing.Music_Playing;

import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

@org.springframework.boot.autoconfigure.SpringBootApplication
@EnableScheduling
public class MinWin {

	// <2021年6月20日>
	PlayList playList;// 右边的播放列表界面
	MusicInfo musicInfo;// 左下角的歌曲信息
	// <2021年6月20日>
	protected Shell shell;
	protected Display display;
	// 记录鼠标的位置信息，用于实习主窗体的拖动
	private boolean isLiteMod;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
//		IPJob.proxies.add(new Proxy("139.213.140.226", 4345));
//		IPJob.proxies.add(new Proxy("110.89.213.123", 4378));
//		IPJob.proxies.add(new Proxy("110.81.49.55", 4352));
//		
//		new Thread() {
//			@Override
//			public void run() {
//				SpringApplication.run(tt.class, args);
//			}
//		}.start();

		try {
			MinWin window = new MinWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new NO_TRIM_MOVABLE_SHELL(SWT.NO_TRIM) {
		};
		shell.setFocus();
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		display = shell.getDisplay();
		shell.setLocation(new Point(320, 140));
		shell.setSize(new Point(1280, 800));
		shell.setRegion(ui.global.GraphicsUtil.createRoundedRectangle(0, 0, 1280, 800, 30));
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setText("\u4E18\u4E18\u64AD\u653E\u5668");
		shell.setImage(SWTResourceManager.getImage(MinWin.class, "/image/logo.png"));
		shell.setLayout(null);

		// 创建搜索界面
		SearchUI searchUI = new SearchUI(shell, SWT.NONE);

		// searchUI.title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 23,
		// SWT.BOLD));
		// 创建音乐馆界面
		MusicStationUI musicStationUI = new MusicStationUI(shell, SWT.NONE);

		// 创建收藏界面
		FavoriteUI favoriteUI = new FavoriteUI(shell, SWT.NONE);

		// 创建本地音乐界面
		LocalMusicUI localMusicUI = new LocalMusicUI(shell, SWT.NONE);

		// 创建最近播放
		RecentMusicUI recentMusicUI = new RecentMusicUI(shell, SWT.NONE);

		// 创建专辑界面
		MusicListUI musicListUI = new MusicListUI(shell, SWT.NONE);

		// 创建登录界面
		LoginUI loginUI = new LoginUI(shell);

		// 创建用户信息界面
		UserInfoUI userInfoUI = new UserInfoUI(shell);

		ui.global.Current.setUserInfoUI(userInfoUI);
		ui.global.Current.setLoginUI(loginUI);
		ui.global.Current.setMusicListUI(musicListUI);
		ui.global.Current.setRecentMusicUI(recentMusicUI);
		ui.global.Current.setLocalMusicUI(localMusicUI);
		ui.global.Current.setFavoriteUI(favoriteUI);
		ui.global.Current.setSearchUI(searchUI);
		ui.global.Current.setMusicStationUI(musicStationUI);
		ui.global.Current.setUI(musicStationUI);
		ui.global.Current.getUI().setVisible(true);

		// 创建歌词显示
		LrcView lrcView = new LrcView(shell, SWT.NONE);
		lrcView.open();
		lrcView.setVisible(false);

		// 创建ui右边的菜单栏
		Composite leftBar = new Composite(shell, SWT.NONE);
		leftBar.setBounds(0, 0, 70, 800);
		leftBar.setBackground(new Color(display, 250, 250, 250));
		leftBar.setLayout(null);

		// 创建左上角图标
		Label appIcon = new Label(leftBar, SWT.CENTER);
		appIcon.setBounds(3, 3, 64, 64);
		appIcon.setImage(SWTResourceManager.getImage(MinWin.class, "/image/logo.png"));
		// 创建音乐馆标签
		LeftButton musicStation = new LeftButton(leftBar, SWT.NONE);
		musicStation.initail("src\\image\\music_station_deactive.png", "src\\image\\music_station_active.png",
				"src\\image\\music_station_actived.png");
		musicStation.setBounds(0, 130, 64, 64);
		musicStation.setBandingUI(musicStationUI);
		LeftButton.currentButton = musicStation;
		ui.global.Current.setPlaySationButton(musicStation);
		musicStation.active();
		// 创建“我的最爱”按钮
		LeftButton favourite = new LeftButton(leftBar, SWT.NONE);
		favourite.initail("src\\image\\favourite_deactive.png", "src\\image\\favourite_active.png",
				"src\\image\\favourite_actived.png");
		favourite.setBounds(0, 220, 64, 64);
		favourite.setBandingUI(favoriteUI);
		// 创建本地音乐按钮
		LeftButton localMusic = new LeftButton(leftBar, SWT.NONE);
		localMusic.initail("src\\image\\local_music_deactive.png", "src\\image\\local_music_active.png",
				"src\\image\\local_music_actived.png");
		localMusic.setBounds(0, 310, 64, 64);
		localMusic.setBandingUI(localMusicUI);
		// 创建最近音乐按钮
		LeftButton recentMusic = new LeftButton(leftBar, SWT.NONE);
		recentMusic.initail("src\\image\\recent_music_deactive.png", "src\\image\\recent_music_active.png",
				"src\\image\\recent_music_actived.png");
		recentMusic.setBounds(0, 400, 64, 64);
		recentMusic.setBandingUI(recentMusicUI);
		// 创建用户按钮
		LeftButton user = new LeftButton(leftBar, SWT.NONE) {
			@Override
			void active() {
				if (currentButton != null) {
					currentButton.deactive();

				}
				ui.global.Current.getUI().setVisible(false);
				currentButton = thisObj;
				if (ui.global.Current.getCurrentUser() != null) {
					bandingUI = ui.global.Current.getUserInfoUI();
				} else {
			
					//bandingUI = ui.global.Current.getUserInfoUI();
					
					bandingUI = ui.global.Current.getLoginUI();
				}
				bandingUI.active();
				ui.global.Current.setUI(bandingUI);
				setImage(actived);
			}
		};
		user.initail("src\\image\\user_deactive.png", "src\\image\\user_active.png", "src\\image\\user_actived.png");
		user.setBounds(0, 490, 64, 64);
		user.setBandingUI(ui.global.Current.getLoginUI());

//		LeftButton user = new LeftButton(leftBar, SWT.NONE);
//		user.initail("src\\image\\user_deactive.png", "src\\image\\user_active.png", "src\\image\\user_actived.png");
//		user.setBounds(0, 490, 64, 64);
		// 创建关闭按钮
		Label closeButton = new Label(leftBar, SWT.NONE);
		closeButton.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				closeButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/close_active.png"));
			}

			@Override
			public void mouseExit(MouseEvent e) {
				closeButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/close_deactive.png"));
			}
		});
		closeButton.addMouseListener(new MouseAdapter() {
			@SuppressWarnings("deprecation")
			@Override
			public void mouseUp(MouseEvent e) {
				closeButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/close_actived.png"));
				try {
					Music_Playing.closeMusic();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if(MusicStationUI.thread != null) {
					MusicStationUI.thread.stop();
				}
				
				shell.close();
			
				
				// closeButton.setImage(SWTResourceManager.getImage(MinWin.class,
				// "/image/close_deactive.png"));
			}
		});
		closeButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/close_deactive.png"));
		closeButton.setBounds(0, 670, 64, 64);
		// 创建精简模式按钮
		Label liteModButton = new Label(leftBar, SWT.NONE);
		liteModButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (isLiteMod) {
					isLiteMod = false;
					liteModButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/streamline_active.png"));
					// TODO
				} else {
					isLiteMod = true;
					liteModButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/streamline_actived.png"));
					// TODO
				}
			}
		});
		liteModButton.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				if (!isLiteMod) {
					liteModButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/streamline_active.png"));
				}
			}

			@Override
			public void mouseExit(MouseEvent e) {
				if (!isLiteMod) {
					liteModButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/streamline_deactive.png"));
				}
			}
		});
		liteModButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/streamline_deactive.png"));
		liteModButton.setBounds(0, 580, 64, 64);
		// 创建底部栏
		Composite bottomBar = new Composite(shell, SWT.NONE);
		bottomBar.setBounds(70, 704, 1210, 96);
		bottomBar.setBackgroundMode(SWT.INHERIT_DEFAULT);
		bottomBar.setLayout(new FormLayout());

		//创建播放暂停按钮
		SwitchButton play_pasueButton = new SwitchButton(bottomBar, SWT.CENTER) {
			@Override
			public void mouseUp(MouseEvent e) {
				super.mouseUp(e);
				if (state) {
					ui.global.Operation._continue();
				}else {
					ui.global.Operation.pause();
				}
			}
		};
		play_pasueButton.Initial(
				SWTResourceManager.getImage(MinWin.class, "/image/pause_button.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/pause_button.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/player_button.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/player_button.png"), false);
		FormData fd_play_pasueButton = new FormData();
		fd_play_pasueButton.top = new FormAttachment(0, 24);
		fd_play_pasueButton.left = new FormAttachment(0, 581);
		play_pasueButton.setLayoutData(fd_play_pasueButton);
		ui.global.Operation.setPlay_pauseButton(play_pasueButton);
		
		

		// 创建上一首按钮
		Label previousButton = new Label(bottomBar, SWT.NONE);
		FormData fd_previousButton = new FormData();
		fd_previousButton.top = new FormAttachment(0, 32);
		fd_previousButton.left = new FormAttachment(0, 543);
		previousButton.setLayoutData(fd_previousButton);
		previousButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ui.global.Operation.preiousMusic();
			}
		});
		previousButton.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				previousButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/previous_active.png"));
			}

			@Override
			public void mouseExit(MouseEvent e) {
				previousButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/previous_deactive.png"));
			}
		});
		previousButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/previous_deactive.png"));

		// 创建下一首按钮
		Label nextButton = new Label(bottomBar, SWT.NONE);
		FormData fd_nextButton = new FormData();
		fd_nextButton.top = new FormAttachment(0, 32);
		fd_nextButton.left = new FormAttachment(0, 635);
		nextButton.setLayoutData(fd_nextButton);
		nextButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ui.global.Operation.nextMusic();
			}
		});
		nextButton.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				nextButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/next_active.png"));
			}

			@Override
			public void mouseExit(MouseEvent e) {
				nextButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/next_deactive.png"));
			}
		});
		nextButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/next_deactive.png"));

		// 创建音乐封面
		Label musicCover = new Label(bottomBar, SWT.NONE);
		FormData fd_musicCover = new FormData();
		fd_musicCover.bottom = new FormAttachment(0, 80);
		fd_musicCover.right = new FormAttachment(0, 80);
		fd_musicCover.top = new FormAttachment(0, 16);
		fd_musicCover.left = new FormAttachment(0, 16);
		musicCover.setLayoutData(fd_musicCover);
		musicCover.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				// musicCover.setImage(SWTResourceManager.getImage(MinWin.class,
				// "/image/lyrics_button.png"));
			}

			@Override
			public void mouseExit(MouseEvent e) {
				// musicCover.setImage(null);
			}
		});
		musicCover.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				// TODO 创建一个歌词的界面
			}
		});
		musicCover.setBackgroundImage(null);

		// 创建歌曲信息块
		musicInfo = new MusicInfo(bottomBar, SWT.NONE);
		FormData fd_musicInfo = new FormData();
		fd_musicInfo.bottom = new FormAttachment(0, 44);
		fd_musicInfo.right = new FormAttachment(0, 470);
		fd_musicInfo.top = new FormAttachment(0, 24);
		fd_musicInfo.left = new FormAttachment(0, 86);
		musicInfo.setLayoutData(fd_musicInfo);

		// 创建收藏按钮
		SwitchButton favorite = new SwitchButton(bottomBar, SWT.NONE) {
			@Override
			public void mouseUp(MouseEvent e) {
				super.mouseUp(e);
				if (state) {
					ui.global.Operation.favorite(ui.global.Operation.getPlayList().getCurrentMusic());
				} else {
					ui.global.Operation.unFavorite(ui.global.Operation.getPlayList().getCurrentMusic());
				}
			}
		};
		favorite.Initial(SWTResourceManager.getImage(MinWin.class, "/image/favorite_actived.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/favorite_actived.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/favorite_deactive.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/favorite_active.png"), false);
		favorite.setSize(new Point(16, 16));
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.top = new FormAttachment(play_pasueButton, -17);
		fd_lblNewLabel.bottom = new FormAttachment(play_pasueButton, 0, SWT.BOTTOM);
		fd_lblNewLabel.right = new FormAttachment(musicCover, 25, SWT.RIGHT);
		fd_lblNewLabel.left = new FormAttachment(musicCover, 6);
		favorite.setLayoutData(fd_lblNewLabel);

		// 创建下载按钮
		SwitchButton download = new SwitchButton(bottomBar, SWT.NONE) {
			@Override
			public void mouseUp(MouseEvent e) {
				super.mouseUp(e);
				if (state) {
					ui.global.Operation.downLoad(ui.global.Operation.getPlayList().getCurrentMusic());
				} else {
					state = !state;
					active();
				}
			}
		};
		download.Initial(SWTResourceManager.getImage(MinWin.class, "/image/download_complete.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/download_complete.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/download_deactive.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/download_active.png"), false);
		download.setSize(new Point(16, 16));
		download.setImage(SWTResourceManager.getImage(MinWin.class, "/image/download_deactive.png"));
		FormData fd_download = new FormData();
		fd_download.bottom = new FormAttachment(play_pasueButton, 0, SWT.BOTTOM);
		fd_download.left = new FormAttachment(favorite, 6);
		download.setLayoutData(fd_download);

		// 创建播放进度条
		PlayProgressBar playProgressBar = new PlayProgressBar(shell, SWT.NONE);
		playProgressBar.setBounds(70, 688, 1210, 16);
		// 创建音量按钮
		VolumeButton volumeButton = new VolumeButton(bottomBar, SWT.NONE);
		FormData fd_volumeButton = new FormData();
		fd_volumeButton.bottom = new FormAttachment(previousButton, 0, SWT.BOTTOM);
		fd_volumeButton.top = new FormAttachment(previousButton, 0, SWT.TOP);
		fd_volumeButton.right = new FormAttachment(nextButton, 73, SWT.RIGHT);
		fd_volumeButton.left = new FormAttachment(nextButton, 41);
		volumeButton.setLayoutData(fd_volumeButton);

		// 创建音量条
		VolumeBar volumeBar = new VolumeBar(bottomBar, SWT.NONE);
		FormData fd_volumeBar = new FormData();
		fd_volumeBar.right = new FormAttachment(volumeButton, 95, SWT.RIGHT);
		fd_volumeBar.bottom = new FormAttachment(previousButton, -11, SWT.BOTTOM);
		fd_volumeBar.top = new FormAttachment(0, 42);
		fd_volumeBar.left = new FormAttachment(volumeButton, 6);
		volumeBar.setLayoutData(fd_volumeBar);
		volumeBar.progress = 1.0;

		// 显示播放时间
		TimeView timeView = new TimeView(bottomBar, SWT.NONE);
		FormData fd_timeView = new FormData();
		fd_timeView.top = new FormAttachment(volumeBar, 0, SWT.TOP);
		timeView.setLayoutData(fd_timeView);
		// 创建歌词按钮
		LrcButton lrcButton = new LrcButton(bottomBar, SWT.NONE);
		fd_timeView.right = new FormAttachment(lrcButton, -51);
		lrcButton.initial(SWTResourceManager.getImage(MinWin.class, "/image/float_lrc_button_deactive.png"),
				SWTResourceManager.getImage(MinWin.class, "/image/float_lrc_button_active.png"));
		lrcButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/float_lrc_button_deactive.png"));
		FormData fd_lrcButton = new FormData();
		fd_lrcButton.left = new FormAttachment(0, 1089);
		fd_lrcButton.top = new FormAttachment(previousButton, 8, SWT.TOP);
		fd_lrcButton.bottom = new FormAttachment(play_pasueButton, 0, SWT.BOTTOM);
		lrcButton.setLayoutData(fd_lrcButton);

		// <2021年6月20日>
		// 创建右边播放列表视图
		playList = new PlayList(shell, SWT.NONE);
		playList.setBounds(1280, 0, 350, 800);
		playList.moveAbove(bottomBar);
		// <2021年6月20日>

		// 创建播放列表按钮
		Label playlistButton = new Label(bottomBar, SWT.NONE);
		playlistButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				playList.pop();
			}
		});
		playlistButton.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				playlistButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/playlist_button_active.png"));
			}

			@Override
			public void mouseExit(MouseEvent e) {
				playlistButton
						.setImage(SWTResourceManager.getImage(MinWin.class, "/image/playlist_button_deactive.png"));
			}
		});
		fd_lrcButton.right = new FormAttachment(playlistButton, -31);
		playlistButton.setImage(SWTResourceManager.getImage(MinWin.class, "/image/playlist_button_deactive.png"));
		FormData fd_playlistButton = new FormData();
		fd_playlistButton.bottom = new FormAttachment(play_pasueButton, 0, SWT.BOTTOM);
		fd_playlistButton.right = new FormAttachment(100, -26);
		playlistButton.setLayoutData(fd_playlistButton);

		// 创建搜索栏
		SearchBar searchBar = new SearchBar(shell, SWT.NONE);
		searchBar.setLocation(120, 10);

		bottomBar.layout();

		ui.global.Operation.setPlayList(playList);
		ui.global.Operation.setSearchBar(searchBar);
		ui.global.Operation.setMusicInfo(musicInfo);
		ui.global.Operation.setPlayProgressBar(playProgressBar);
		ui.global.Operation.setVolumeBar(volumeBar);
		ui.global.Operation.setTimeView(timeView);
		ui.global.Operation.setMusicCover(musicCover);
		ui.global.Operation.setFavoriteButton(favorite);
		ui.global.Operation.setDownloadButton(download);
		ui.global.Operation.setLrcView(lrcView);

		// musicStation.active();
		playList.moveAbove(null);

	}

}
